﻿using ExtensionTools;
using ExtensionTools.MD5Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using NewsSevicer.Interface;
using NewsSevicer.ResultClass;
using NewsSevicer.ResultClass.InputDto;
using System;
using System.Collections.Generic;

namespace NewsWebsite.Controllers
{
    [Route("api/OnLineUser")]
    [ApiController]
    public class OnLineUserController : BaseApiController
    {
       private  IOnlineUserService _onLineUserService;
        public OnLineUserController(IOnlineUserService onlineUserService) { 
        
            _onLineUserService = onlineUserService;
        }

        [HttpPost("addUser")]
        public ResultData RegisterUser([FromBody] RegisterUserDto registerUserDto) {
           var user = DtoExtensions.ToEntity(registerUserDto);
           user.Password = MD5Helper.EncryptMD5(user.Password);
           var res  =    _onLineUserService.CreateUser(user);
            if (res != null) {
                return new ResultData { 
                code=0,
                message="",
                data=null
                };
            }

            return new ResultData
            {
                code = 1,
                message = "",
                data = null
            };
        }


        [HttpPost("login")]
        public RequestResult<loginInfoDto> login([FromBody] LoignUserDto loignUserDto) {

            var userIP = HttpContext.Connection.RemoteIpAddress;
            if (userIP != null)
            {
                // 如果是IPv4地址，直接返回字符串形式的IP
                if (userIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                    var a = userIP.ToString();
                }
                // 如果是IPv6地址，可能需要进一步处理
                else if (userIP.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                {
                    // 这里简单地返回压缩格式的IPv6地址
                    var a = userIP.MapToIPv4().ToString();
                }
            }
                if (loignUserDto==null )
            {
                throw new Exception("登录信息不能为空");
            }
            var user = DtoExtensions.ToEntity(loignUserDto);
            user.Password = MD5Helper.EncryptMD5(user.Password);
          return  _onLineUserService.Login(user);


        }

        [HttpGet("getMenu")]
        public IActionResult GetMenu()
        {
            var response = new
            {
                code = 0,
                msg = "Success",
                data = new List<object>
                {
                    new {
                        id = "/workspace",
                        icon = "layui-icon-home",
                        title = "工作空间",
                        children = new List<object>
                        {
                            new { id = "/workspace/workbench", icon = "layui-icon-util", title = "工作台" },
                            new { id = "/workspace/console", icon = "layui-icon-engine", title = "控制台" },
                            new { id = "/workspace/analysis", icon = "layui-icon-chart-screen", title = "分析页" },
                            new { id = "/workspace/monitor", icon = "layui-icon-find-fill", title = "监控页" }
                        }
                    },
                    new {
                        id = "/form",
                        icon = "layui-icon-table",
                        title = "表单页面",
                        children = new List<object>
                        {
                            new { id = "/form/base", icon = "layui-icon-form", title = "基础表单" },
                            new { id = "/form/intricate", icon = "layui-icon-form", title = "复杂表单" },
                            new { id = "/form/step", icon = "layui-icon-form", title = "分步表单" }
                        }
                    },
                    new {
                        id = "/table",
                        icon = "layui-icon-align-left",
                        title = "列表页面",
                        children = new List<object>
                        {
                            new { id = "/table/base", icon = "layui-icon-search", title = "查询列表" },
                            new { id = "/table/card", icon = "layui-icon-file-b", title = "卡片列表" },
                            new { id = "/table/project", icon = "layui-icon-templeate-one", title = "项目列表" },
                            new { id = "/table/article", icon = "layui-icon-carousel", title = "文章列表" }
                        }
                    },
                    new {
                        id = "/result",
                        icon = "layui-icon-template",
                        title = "结果页面",
                        children = new List<object>
                        {
                            new { id = "/result/success", icon = "layui-icon-success", title = "成功页面" },
                            new { id = "/result/failure", icon = "layui-icon-error", title = "失败页面" }
                        }
                    },
                    new {
                        id = "/error",
                        icon = "layui-icon-unlink",
                        title = "异常页面",
                        children = new List<object>
                        {
                            new { id = "/error/403", icon = "layui-icon-not-found", title = "403" },
                            new { id = "/error/404", icon = "layui-icon-not-found", title = "404" },
                            new { id = "/error/500", icon = "layui-icon-not-found", title = "500" }
                        }
                    },
                    new {
                        id = "/menu",
                        icon = "layui-icon-app",
                        title = "菜单嵌套",
                        children = new List<object>
                        {
                            new {
                                id = "/menu/menu1",
                                icon = "layui-icon-component",
                                title = "二级菜单",
                                children = new List<object>
                                {
                                    new { id = "/menu/menu1/menu1", icon = "layui-icon-template-one", title = "三级菜单" },
                                    new { id = "/menu/menu1/menu2", icon = "layui-icon-template-one", title = "三级菜单" }
                                }
                            },
                            new {
                                id = "/menu/menu2",
                                icon = "layui-icon-component",
                                title = "二级菜单",
                                children = new List<object>
                                {
                                    new { id = "/menu/menu2/menu1", icon = "layui-icon-template-one", title = "三级菜单" },
                                    new { id = "/menu/menu2/menu2", icon = "layui-icon-template-one", title = "三级菜单" }
                                }
                            }
                        }
                    },
                    new {
                        id = "/directive",
                        icon = "layui-icon-test",
                        title = "内置指令",
                        children = new List<object>
                        {
                            new { id = "/directive/permission", icon = "layui-icon-template", title = "权限指令" }
                        }
                    },
                    new {
                        id = "/page",
                        icon = "layui-icon-link",
                        title = "外链页面",
                        children = new List<object>
                        {
                            new { id = "layui-icon-layer", icon = "layui-icon-home", title = "弹层外链", type = "modal" },
                            new { id = "http://www.baidu.com", icon = "layui-icon-layouts", title = "原生跳转", type = "blank" }
                        }
                    },
                    new {
                        id = "/enrollee",
                        icon = "layui-icon-slider",
                        title = "个人中心",
                        children = new List<object>
                        {
                            new { id = "/enrollee/profile", icon = "layui-icon-username", title = "我的资料" },
                            new { id = "/enrollee/message", icon = "layui-icon-email", title = "我的消息" }
                        }
                    },
                    new {
                        id = "/system",
                        icon = "layui-icon-set",
                        title = "系统管理",
                        children = new List<object>
                        {
                            new { id = "/system/user", icon = "layui-icon-user", title = "用户管理" },
                            new { id = "/system/role", icon = "layui-icon-user", title = "角色管理" },
                            new { id = "/system/menu", icon = "layui-icon-spread-left", title = "菜单管理" },
                            new { id = "/system/organization", icon = "layui-icon-transfer", title = "机构管理" },
                            new { id = "/system/dictionary", icon = "layui-icon-read", title = "字典管理" },
                            new { id = "/system/file", icon = "layui-icon-file", title = "文件管理" },
                            new { id = "/system/login", icon = "layui-icon-date", title = "登录日志" },
                            new { id = "/system/option", icon = "layui-icon-survey", title = "操作日志" }
                        }
                    }
                }
            };

            return Ok(response);
        }

        [HttpGet("getPermissions")]
        public IActionResult GetPermissions()
        {
            var response = new
            {
                code = 0,
                msg = "Success",
                data = new List<string>
                {
                    "sys:user:add",
                    "sys:user:edit",
                    "sys:user:delete",
                    "sys:user:import",
                    "sys:user:export"
                }
            };

            return Ok(response);
        }

    }
}
